<template>
	<view class="sleep" :style="{paddingTop: paddingHeight + 'px'}">
		<nav-bar></nav-bar>
		<view class="title">
			睡眠时间
		</view>
		<view class="c-cantainer">
			<view class="weight" @click="handleModal('down')">
				<u-input shape="circle" inputAlign="right" readonly v-model="sleep">
					<template slot="prefix">
						<text class="prefix">入睡时间</text>
					</template>
				</u-input>
			</view>
			<view class="stature" @click="handleModal('up')">
				<u-input shape="circle" inputAlign="right" readonly v-model="wake">
					<template slot="prefix">
						<text class="prefix">起床时间</text>
					</template>
				</u-input>
			</view>
		</view>
		<view class="next" @click="next">
			<uni-icons type="arrow-right" color="#fffF" size="50"></uni-icons>
		</view>
		<u-datetime-picker :show="showModal" @cancel="showModal = false" @confirm="setTime"
			mode="time"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		saveUserMsgByUserId
	} from '@/api/index.js'
	export default {
		data() {
			return {
				paddingHeight: uni.getStorageSync('navbarHeight'),
				userId: uni.getStorageSync('userId'),
				imgUrl: this.$imgUrl,
				showModal: false,
				type: '',
				userInfo: {
					sleepHour: null,
					sleepMin: null,
					wakeHour: null,
					wakeMin: null,
				},
				sleep: undefined,
				wake: undefined
			}
		},
		onLoad(OPTIONS) {
			console.log('OPTIONS', OPTIONS)
			this.userInfo = Object.assign({}, this.userInfo, JSON.parse(OPTIONS.userInfo))
		},
		methods: {
			handleModal(type) {
				this.type = type
				this.showModal = true
			},
			setTime(val) {
				console.log(val)
				if (this.type == 'down') {
					this.sleep = val.value
					this.userInfo.sleepHour = val.value.split(':')[0]
					this.userInfo.sleepMin = val.value.split(':')[1]
				} else {
					this.wake = val.value
					this.userInfo.wakeHour = val.value.split(':')[0]
					this.userInfo.wakeMin = val.value.split(':')[1]
				}
				this.showModal = false
			},
			async next() {
				if (!this.userInfo.wakeHour || !this.userInfo.sleepHour) {
					uni.showToast({
						title: '请填写信息',
						icon: 'none'
					})
					return
				}
				let params = {
					...this.userInfo,
					userId: this.userId
				}
				try {
					const res = await saveUserMsgByUserId(params)
					console.log(res)
					if (res.code == 200) {
						uni.reLaunch({
							url: '/pagesB/login/device/search/index?type=login'
						})
					}
				} catch (e) {
					//TODO handle the exception
				}

			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f7f7f7;
	}
</style>
<style lang="scss" scoped>
	.sleep {
		display: flex;
		align-items: flex-start;
		justify-content: center;
		flex-direction: column;
		padding: 0 80rpx 0;

		.title {
			font-size: 48rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
			margin-top: 45rpx;
			letter-spacing: 1px;
		}

		.sub_title {
			font-size: 32rpx;
			color: #333333;
			letter-spacing: 0.67px;
			margin-top: 16rpx;
		}

		.c-cantainer {
			margin-top: 176rpx;
			width: 100%;

			/deep/.u-input {
				padding: 0 50rpx 0 !important;
				height: 112rpx;
				background-color: #FAFAFA;
				margin-top: 48rpx;

				.u-input__content {
					line-height: 112rpx !important;
				}
			}
		}

		.gender {
			width: 100%;
			margin-top: 48rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.woman,
			.man {
				width: 274rpx;
				height: 112rpx;
			}

			.woman {
				margin-left: 44rpx;
			}
		}
	}
</style>